$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("[name='khwl_gs']").jgbmtree($("[name='khwl_bm']"), '', '', '');
   // $("[name='khwl_kh']").qyjgzcombobox();
    //搜索按钮
    $("#khwl_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams:{
                gsid:$("#khwl_toolbar [name='khwl_gs']").val(),
                deptid:$("#khwl_toolbar [name='khwl_bm']").val(),
                enddt:$("#khwl_toolbar [name='khwl_endDate']").val(),
                custname:$("#khwl_toolbar [name='khwl_kh']").val(),
                tc_flag:$("#khwl_toolbar [name='khwl_tc']").val()
            }});
    });


//======================
//========统计列表========
//======================
    var columns = [
        [
            {field:'gs_name', title:'公司名称', rowspan:2, width:145, align:'center'},
            {field:'cust_name', title:'客户', rowspan:2, width:250, align:'center'},
            {title:'业务', colspan:2, width:100, align:'right'},
            {title:'财务', colspan:4, width:100, align:'right'}
        ],
        [
            {field:'yw_yusk', title:'预收款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='yw_yusk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'yw_yingsk', title:'应收款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='yw_yingsk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'xswjs_sl', title:'销售未结算数量', width:145, formatter:function (v, r, i) {
                return formatSl2(v, ",", ".");
            }, align:'right'},
            {field:'xswjs_je', title:'销售未结算金额', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='xswjsje' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'cw_yusk', title:'预收款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='cw_yusk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'},
            {field:'cw_yingsk', title:'应收款', width:145, formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='cw_yingsk' gsid='{1}' custname='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.cust_name, formatSl2(v, ",", "."));
                } else {
                    return formatSl2(v, ",", ".");
                }
            }, align:'right'}
        ]
    ];

    var grid = $('#khwl_grid').datagrid({
        url:$.webPath() + '/tjbb/khwl/khwlList',
        toolbar:"#khwl_toolbar",
        pagination:true,
        pageSize:100,
        rownumbers:true,
        singleSelect:true,
        collapsible:true,
        fit:true,
        fitColumns:true,
        pageNumber:1,
        showFooter:true,
        onLoadSuccess:function () {
            //
            $(".xswjsje").unbind("click").bind("click", function () {
                khwlDzShowDetail1('销售未结算金额', $.webPath() + "/tjbb/khwl/xswjsje", $(this).attr("gsid"), $(this).attr("custname"), 1);
            });
            $(".yw_yusk").unbind("click").bind("click", function () {
                khwlDzShowDetail('客户往来对账', $.webPath() + "/tjbb/khwl/khwldz", $(this).attr("gsid"), $(this).attr("custname"), 1);
            });
            $(".yw_yingsk").unbind("click").bind("click", function () {
                khwlDzShowDetail('客户往来对账', $.webPath() + "/tjbb/khwl/khwldz", $(this).attr("gsid"), $(this).attr("custname"), 1);
            });
            $(".cw_yusk").unbind("click").bind("click", function () {
                khwlDzShowDetail('客户往来对账', $.webPath() + "/tjbb/khwl/khwldz", $(this).attr("gsid"), $(this).attr("custname"), 2);
            });
            $(".cw_yingsk").unbind("click").bind("click", function () {
                khwlDzShowDetail('客户往来对账', $.webPath() + "/tjbb/khwl/khwldz", $(this).attr("gsid"), $(this).attr("custname"), 2);
            });

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        columns:columns
    });

    //===========================================
//==============此页面用到的函数===================
//===========================================

    var khwlDzShowDetail = function (title, url, gsid, custname, type) {
        var win = $.window({
            title:title,
            width:1050,
            height:470,
            cache:false,
            iconCls:'icon-tip',
            url:encodeURI(encodeURI(url + "?gsid=" + gsid + "&custname=" + custname + "&type=" + type + "&enddt=" + $("#khwl_endDate").datebox("getValue") + "&deptid=" + $("#khwl_toolbar input[name='khwl_bm']").val())),
            modal:true,
            buttons:[]
        });
    }
    var khwlDzShowDetail1 = function (title, url, gsid, custname, enddt) {
        var win = $.window({
            title:title,
            width:1050,
            height:470,
            cache:false,
            iconCls:'icon-tip',
            url:encodeURI(encodeURI(url + "?gsid=" + gsid + "&custname=" + custname + "&enddt=" + $("#khwl_endDate").datebox("getValue") + "&deptid=" + $("#khwl_toolbar input[name='khwl_bm']").val())),
            modal:true,
            buttons:[]
        });
    }

    //=========================导出=================================
    $("#btn_khwl_export").click(function () {
        window.open($.webPath() + "/tjbb/khwl/khwl_export?gsid=" + $("#khwl_toolbar [name='khwl_gs']").val() + "&deptid=" + $("#khwl_toolbar [name='khwl_bm']").val() + "&enddt=" + $("#khwl_toolbar [name='khwl_endDate']").val() +
            "&custname=" + $("#khwl_toolbar [name='khwl_kh']").val() +
            "&tc_flag=" + $("#khwl_toolbar [name='khwl_tc']").val());
    });
    //==================================================================================
});
